package com.ems.service.impl;

import com.ems.entity.AlarmEntity;
import com.ems.mapper.AlarmMapper;
import com.ems.service.IAlarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.AlarmVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 报警/故障 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-10-24
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, AlarmEntity> implements IAlarmService {

    @Resource
    private AlarmMapper alarmMapper;

    @Override
    public List<AlarmVo> queryList (AlarmVo alarmVo) {
        if ( alarmVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        alarmVo.setPage ( ( alarmVo.getPage () - 1 ) * alarmVo.getLimit () );
        List<AlarmVo> alarmVos = alarmMapper.queryList ( alarmVo );
        return alarmVos;
    }

    @Override
    public Integer queryCount (AlarmVo alarmVo) {
        Integer count = alarmMapper.queryCount ( alarmVo );
        return count;
    }

    @Override
    public AlarmVo saveSingle (AlarmVo alarmVo) {
        if ( alarmVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        AlarmEntity alarmEntity = new AlarmEntity ();
        BeanUtils.copyProperties ( alarmVo, alarmEntity );
        alarmEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        alarmEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( alarmEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return alarmVo;
    }

    @Override
    public AlarmVo querySingle (AlarmVo alarmVo) {
        //1. 查单个报警详情
        if ( alarmVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( alarmVo.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        AlarmVo alarm = alarmMapper.querySingle ( alarmVo );
        return alarm;
    }

}
